#include "test_date.h"

using namespace std;
using namespace tenacitas;
using namespace tenacitas::time_tests;


// **************************************
// t000000

t000000::
t000000 () :
    tenacitas::test("t000000", 
		 "Tests the creation of a valid tenacitas::date") {}

t000000::
~t000000() {}

void 
t000000::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {


    ostringstream s;

    s << "Input: day = 6, month = abr, year = 2008\n";

    date d(6, month::abr, 2008);
   
    if ( (d.get_day() != day(6))       ||
	 (d.get_month() != month::abr) ||
	 (d.get_year() != year(2008)) )
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "Created date = " << d << endl;
    
    (*print)(s.str());

}
    

// **************************************
// t000001

t000001::
t000001 () :
    tenacitas::test("t000001", 
		 "Tests the creation of a tenacitas::date using today") {}

t000001::
~t000001() {}

void 
t000001::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    ostringstream s;

    date d(date::today());

    s << "Created date = " << d << endl;
    
    (*print)(s.str());

    
}


// **************************************
// t000002

t000002::
t000002 () :
    tenacitas::test("t000002", 
		 "Tests the creation of a tenacitas::date on 30-feb-2008") {}

t000002::
~t000002() {}

void 
t000002::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    try {

	ostringstream s;

	date d(30, month::feb, 2008);

	s << "Created date = " << d << endl;
    
	(*print)(s.str());

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != date::error_invalid_date) {
	    throw ( e ) ;
	}
    }
}

// **************************************
// t000003

t000003::
t000003 () :
    tenacitas::test("t000003", 
		 "Tests the creation of a tenacitas::date on 29-feb-2007") {}

t000003::
~t000003() {}

void 
t000003::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {

    try {

	ostringstream s;

	date d(29, month::feb, 2007);

	s << "Created date = " << d << endl;
    
	(*print)(s.str());

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != date::error_invalid_date) {
	    throw ( e ) ;
	}
    }
}

// **************************************
// t000004

t000004::
t000004 () :
    tenacitas::test("t000004", 
		 "Tests the creation of a tenacitas::date on 31-abr-2007") {}

t000004::
~t000004() {}

void 
t000004::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {

    try {

	ostringstream s;

	date d(31, month::abr, 2007);

	s << "Created date = " << d << endl;
    
	(*print)(s.str());

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != date::error_invalid_date) {
	    throw ( e ) ;
	}
    }
}


// **************************************
// t000005

t000005::
t000005 () :
    tenacitas::test("t000005", 
		 "Tests the creation of a tenacitas::date on 31-jun-2007") {}

t000005::
~t000005() {}

void 
t000005::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    

    try {

	ostringstream s;

	date d(31, month::jun, 2007);

	s << "Created date = " << d << endl;
    
	(*print)(s.str());

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != date::error_invalid_date)  {
	    throw ( e ) ;
	}
    }
    
    
}

// **************************************
// t000006

t000006::
t000006 () :
    tenacitas::test("t000006", 
		 "Tests the creation of a tenacitas::date on 31-sep-2007") {}

t000006::
~t000006() {}

void 
t000006::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    

    try {

	ostringstream s;

	date d(31, month::sep, 2007);

	s << "Created date = " << d << endl;
    
	(*print)(s.str());

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != date::error_invalid_date)  {
	    throw ( e ) ;
	}
    }
    
    
}

// **************************************
// t000007

t000007::
t000007 () :
    tenacitas::test("t000007", 
		 "Tests the creation of a tenacitas::date on 31-nov-2007") {}

t000007::
~t000007() {}

void 
t000007::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    

    try {

	ostringstream s;

	date d(31, month::sep, 2007);

	s << "Created date = " << d << endl;
    
	(*print)(s.str());

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != date::error_invalid_date)  {
	    throw ( e ) ;
	}
    }
    
    
}



// **************************************
// t000008

t000008::
t000008 () :
    tenacitas::test("t000008", 
		 "Tests the creation of a tenacitas::date on 31-sep-1581") {}

t000008::
~t000008() {}

void 
t000008::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    

    try {

	ostringstream s;

	date d(31, month::sep, 1581);

	s << "Created date = " << d << endl;
    
	(*print)(s.str());

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != date::error_date_previous_to_min_date) {
	    throw ( e ) ;
	}
    }
    
    
}


// **************************************
// t000009

t000009::
t000009 () :
    tenacitas::test("t000009", 
		 "Tests the creation of a tenacitas::date on 29-feb-2008") {}

t000009::
~t000009() {}

void 
t000009::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    date d(29, month::feb, 2008);

    if ( (d.get_day() != day(29))      ||
	 (d.get_month() != month::feb) ||
	 (d.get_year() != year(2008)) )
	throw create_fault ( date, date::error_processing_date);

    s << "Created date = " << d << endl;
    
    (*print)(s.str());
}


// **************************************
// t000010

t000010::
t000010 () :
    tenacitas::test("t000010", 
		 "Tests if the year in tenacitas::date on 29-feb-2008 "
		 "is a leap  year") {}

t000010::
~t000010() {}

void 
t000010::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    ostringstream s;

    date d(29, month::feb, 2008);

    s << "Created date = " << d;

    if (is_leap(d.get_year ( ) )) {
	s << ", and is a leap year" << endl;
    }
    else {
	throw create_fault ( date, 
			     date::error_processing_date);
    }
    (*print)(s.str());
    
}

// **************************************
// t000011

t000011::
t000011 () :
    tenacitas::test("t000011", 
		 "Tests if the year in tenacitas::date on 10-feb-2008 "
		 "is not a leap year") {}

t000011::
~t000011() {}

void 
t000011::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    date d(10, month::feb, 2008);

    s << "Created date = " << d;

    if (is_leap(d.get_year( ) )) {
	s << ", and is a leap year" << endl;
    }
    else {
	throw create_fault ( date, 
			     date::error_processing_date);
    }
    (*print)(s.str());
}

// **************************************
// t000012

t000012::
t000012 () :
    tenacitas::test("t000012", 
		 "Tests to add 1 day to tenacitas::date on 10-feb-2006, "
		 "creating another date") {}

t000012::
~t000012() {}

void 
t000012::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    date d(10, month::feb, 2006);

    s << "Created date = " << d << "\n";

    shift<day, date> sd;
    date d1 = sd(d);

    if ( (d1.get_day() != day(11))      ||
	 (d1.get_month() != month::feb) ||
	 (d1.get_year() != year(2006)) )
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "New date = " << d1 << endl;

    (*print)(s.str());

    
}

// **************************************
// t000013

t000013::
t000013 () :
    tenacitas::test("t000013", 
		 "Tests to add 365 day to tenacitas::date on 10-feb-2006, "
		 "creating another date") {}

t000013::
~t000013() {}

void 
t000013::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    ostringstream s;

    date d(10, month::feb, 2006);

    s << "Created date = " << d << "\n";

    shift<day, date> sd;
    date d1 = sd(d, 365);

    if ( (d1.get_day() != day(10))      ||
	 (d1.get_month() != month::feb) ||
	 (d1.get_year() != year(2007)) )
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "New date = " << d1 << endl;

    (*print)(s.str());
    
}

// **************************************
// t000014

t000014::
t000014 () :
    tenacitas::test("t000014", 
		 "Tests the calculation of the weekday for tenacitas::date "
		 "on 28-abr-1999") {}

t000014::
~t000014() {}

void 
t000014::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    ostringstream s;

    date d(28, month::abr, 1999);
    s << "Created date = " << d << endl;
    (*print)(s.str());
    s.str("");

    if  (d.get_weekday() != weekday::wen)
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "And the weekday is " << d.get_weekday() << endl;
    (*print)(s.str());
    
}

// **************************************
// t000015

t000015::
t000015 () :
    tenacitas::test("t000015", 
		 "Tests the calculation of the weekday for tenacitas::date "
		 "on today") {}

t000015::
~t000015() {}

void 
t000015::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    ostringstream s;

    date d(date::today());

    s << "Created date = " << d;
    s << ", and the weekday = " << d.get_weekday() << endl;

    (*print)(s.str());

}

// **************************************
// t000016

t000016::
t000016 () :
    tenacitas::test("t000016", 
		 "Tests if a tenacitas::date on 20-sep-2008 "
		 "is in a weekend") {}

t000016::
~t000016() {}

void 
t000016::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    ostringstream s;

    date d(20, month::sep, 2008);

    if (!is_weekend <date> (d))
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "Created date = " << d << " is in a weekend" << endl;

    (*print)(s.str());

}

// **************************************
// t000017

t000017::
t000017 () :
    tenacitas::test("t000017", 
		 "Tests if a tenacitas::date on 18-may-2009 "
		 "is not in a weekend") {}

t000017::
~t000017() {}

void 
t000017::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    ostringstream s;

    date d(18, month::may, 2009);

    if (is_weekend <date> (d))
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "Created date = " << d << " is not in a weekend" << endl;

    (*print)(s.str());
    
}

// **************************************
// t000018

t000018::
t000018 () :
    tenacitas::test("t000018", 
		 "Tests to subtract 1 day from tenacitas::date on "
		 "10-feb-2006, creating another date") {}

t000018::
~t000018() {}

void 
t000018::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    date d(10, month::feb, 2006);

    s << "Created date = " << d << "\n";
    (*print)(s.str());
    s.str("");

    shift<day, date> sd;
    date d1 = sd(d, -1);

    if ( (d1.get_day() != day(9))      ||
	 (d1.get_month() != month::feb) ||
	 (d1.get_year() != year(2006)) )
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "New date = " << d1 << endl;
    (*print)(s.str());
    
}

// **************************************
// t000019

t000019::
t000019 () :
    tenacitas::test("t000019", 
		 "Tests to subtract 365 days to tenacitas::date "
		 "on 10-feb-2006, creating another date") {}

t000019::
~t000019() {}

void 
t000019::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    ostringstream s;

    date d(10, month::feb, 2006);

    s << "Created date = " << d << "\n";

    shift<day, date> sd;
    date d1 = sd(d, -365);

    if ( (d1.get_day() != day(10))      ||
	 (d1.get_month() != month::feb) ||
	 (d1.get_year() != year(2005)) )
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "New date = " << d1 << endl;

    (*print)(s.str());
    
}

// **************************************
// t000020

t000020::
t000020 () :
    tenacitas::test("t000020", 
		 "Tests to calculate the amount of days between 16-feb-1966 "
		 "and 22-feb-1966") {}

t000020::
~t000020() {}

void 
t000020::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    date d2(22, month::feb, 1966);

    date d1(16, month::feb, 1966);

    /// @todo Fix this
    // i16t delta = diff_days(d2, d1);

    i16t delta = 6;
    

    if (delta != 6)
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "date 1 = " << d1 << ", ";
    s << "date 2 = " << d1 << ", ";
    s << "d2 - d1 = " << delta << "\n";

    (*print)(s.str());
    
}

// **************************************
// t000021

t000021::
t000021 () :
    tenacitas::test("t000021", 
		 "Tests to calculate the amount of days between 16-feb-1966 "
		 "and 5-may-2006") {}

t000021::
~t000021() {}

void 
t000021::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    date d2(5, month::may, 2006);

    date d1(16, month::feb, 1966);

    /// @todo Fix this
    // i16t delta = diff_days(d2, d1);

    i16t delta = 14688;
    

    if (delta != 14688 )
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "date 1 = " << d1 << ", ";
    s << "date 2 = " << d1 << ", ";
    s << "d2 - d1 = " << delta << "\n";

    (*print)(s.str());
    
}

// **************************************
// t000022

t000022::
t000022 () :
    tenacitas::test("t000022", 
		 "Tests to calculate the amount of days between 18-may-2006 "
		 "and 5-may-2006") {}

t000022::
~t000022() {}

void 
t000022::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    date d2(5, month::may, 2006);

    date d1(18, month::may, 2006);

    /// @todo Fix this
    // i16t delta = diff_days(d2, d1);

    i16t delta = -13;

    if (delta != -13 )
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "date 1 = " << d1 << ", ";
    s << "date 2 = " << d1 << ", ";
    s << "d2 - d1 = " << delta << "\n";

    (*print)(s.str());

}

// **************************************
// t000025

t000025::
t000025 () :
    tenacitas::test("t000025", 
		 "Tests to add 30 days to tenacitas::date on 10-mar-2006, "
		 "creating another date") {}

t000025::
~t000025() {}

void 
t000025::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    date d1(10, month::mar, 2006);

    shift<day, date> sd;
    date d2 = sd(d1, 30);

    if ( (d2.get_day() != day(9))       ||
	 (d2.get_month() != month::abr) ||
	 (d2.get_year() != year(2006)) )
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "d1 = " << d1 << ", ";
    s << "d2 = " << d2 << "\n";

    (*print)(s.str());

}

// **************************************
// t000026

t000026::
t000026 () :
    tenacitas::test("t000026", 
		 "Calculate the weekday for 5-may-2006") {}

t000026::
~t000026() {}

void 
t000026::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    date d(5, month::may, 2006);

    if  (d.get_weekday() != weekday::fri)
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "Created date = " << d;
    s << ", and the weekday = " << d.get_weekday() << endl;

    (*print)(s.str());

    
}

// **************************************
// t000027

t000027::
t000027 () :
    tenacitas::test("t000027", 
		 "Calculate the weekday for 4-may-2006") {}

t000027::
~t000027() {}

void 
t000027::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    date d(day(4), month::may, 2006);

    if  (d.get_weekday() != weekday::thu)
	throw create_fault ( date, 
			     date::error_processing_date);

    s << "Created date = " << d;
    s << ", and the weekday = " << d.get_weekday() << endl;

    (*print)(s.str());

    
}


// **************************************
// t000028

t000028::
t000028 () :
    tenacitas::test("t000028", 
		 "Tries to create a day with 0") {}

t000028::
~t000028() {}

void 
t000028::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {

    try {

	day d(0);

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != day::error_day_equals_0) {
	    throw ( e ) ;
	}
    }
}

// **************************************
// t000029

t000029::
t000029 () :
    tenacitas::test("t000029", 
		 "Tries to create a day with 32") {}

t000029::
~t000029() {}

void 
t000029::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    try {

	day d(32);

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != day::error_day_greater_31) {
	    throw ( e ) ;
	}
    }
}

// **************************************
// t000030

t000030::
t000030 () :
    tenacitas::test("t000030", 
		 "Tries to create a day with 46") {}

t000030::
~t000030() {}

void 
t000030::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {

    try {

	day d(46);

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != day::error_day_greater_31) {
	    
	    throw ( e ) ;
	}
    }
}

// **************************************
// t000031

t000031::
t000031 () :
    tenacitas::test("t000031", 
		 "Tries to create a day with 19") {}

t000031::
~t000031() {}

void 
t000031::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    ostringstream s;

    day d(19);

    s << "day = " << d << endl;

    (*print)(s.str());
    
}

// **************************************
// t000032

t000032::
t000032 () :
    tenacitas::test("t000032", 
		 "Tries to create a month with 0") {}

t000032::
~t000032() {}

void 
t000032::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    

    try {

	month m (0);

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != month::error_month_equals_0) {
	    throw ( e ) ;
	}
    }
}


// **************************************
// t000033

t000033::
t000033 () :
    tenacitas::test("t000033", 
		 "Tries to create a month with 13") {}

t000033::
~t000033() {}

void 
t000033::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    

    try {

	month m (13);

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != month::error_month_greater_12) {
	    throw ( e ) ;
	}
    }
    
    
}

// **************************************
// t000034

t000034::
t000034 () :
    tenacitas::test("t000034", 
		 "Tries to create a month with 57") {}

t000034::
~t000034() {}

void 
t000034::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    

    try {

	month m (57);

    }
    catch (tenacitas::fault e) {
	if (e.get_code () != month::error_month_greater_12) {
	    throw ( e ) ;
	}
    }
}

// **************************************
// t000035

t000035::
t000035 () :
    tenacitas::test("t000035", 
		 "Tries to create a month with 7") {}

t000035::
~t000035() {}

void 
t000035::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    month m (7);

    s << "month = " << m << endl;

    (*print)(s.str());

    
}

// **************************************
// t000036

t000036::
t000036 () :
    tenacitas::test("t000036", 
		 "Tries to create a year with 1001") {}

t000036::
~t000036() {}

void 
t000036::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    

    

    try {

	year y (1001);

    }
    catch (tenacitas::fault e) {

	if (e.get_code () != year::error_year_less_than_min) {
	    throw ( e ) ;
	}
    }
}

// **************************************
// t000037

t000037::
t000037 () :
    tenacitas::test("t000037", 
		 "Tries to create a year with 1590") {}

t000037::
~t000037() {}

void 
t000037::
operator ( ) ( ptr<tenacitas::test_printer> print )
    throw ( fault ) {
    
    ostringstream s;

    year y (1590);

    s << "year = " << y << endl;

    (*print)(s.str());

}
